<!-- file cabang -->
<script> 
	
	$(function(){
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
			if (this.id && this.id !== '') {
				if (ids[this.id]) {
					$(this).remove();
				} else {
					ids[this.id] = this
				}
			}
		});
		
		/* external function */
		$.fn.fmatter.editBtn = function(cellval, opts) {
			return "<button class='editBtn' data-id='"+cellval+"' >Ubah</button>";
		};
		
		$.fn.fmatter.deleteBtn = function(cellval, opts) {
			return "<button class='deleteBtn' data-id='"+cellval+"' >Hapus</button>";
		};
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_cabang = $("#task_cabang");
		grid_cabang = $("#grid-cabang");
		pgrid_cabang = $("#pgrid-cabang");
		btnadd_cabang = $("#btn-add-cabang");
		btncancel_cabang =  $("#btn-cancel-cabang");
		btnsave_cabang = $("#btn-save-cabang");
		form_cabang = $("#form-cabang");
		
		field_cabang_id = $("#cabang_id");
		field_cabang_nama = $("#cabang_nama");
		field_cabang_aktif = $("#cabang_aktif");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_cabang.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_cabang',
			datatype: "json", 
			colNames:[
				'cabang_nama',
				'cabang_aktif',
				'','','cabang_id'],
			colModel:[
				{ name: 'cabang_nama'  ,index: 'cabang_nama' , search : true},
				{ name: 'cabang_aktif'  ,index: 'cabang_aktif' , search : true},
				{ name: 'cabang_id' ,index: 'cabang_id', width: '75' , search : false, formatter : 'editBtn' },
				{ name: 'cabang_id'  ,index: 'cabang_id',  width: '75', search : false, formatter : 'deleteBtn'},
				{ name: 'cabang_id'  ,index: 'cabang_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-cabang', 
			sortname: 'cabang_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" ,
			loadComplete : function(){
				$( "input[type=submit],input[type=button], button" ).button();
			
				editBtn = $(".editBtn");
				deleteBtn = $(".deleteBtn");
			
				editBtn.click(function(){
					task_cabang.val("UPDATE");
					var data = $(this).data();
					dataRow = grid_cabang.getRowData(data.id);
					field_cabang_id.val(dataRow.cabang_id);
					field_cabang_nama.val(dataRow.cabang_nama);
					field_cabang_aktif.val(dataRow.cabang_aktif);
					form_cabang.dialog("open");
				});
				
				deleteBtn.click(function(){
					confr = confirm("Apa Anda yakin menghapus data ini?");
					if (confr){
						var data = $(this).data();
						$.ajax({
							type: 'POST',
							url: 'c_cabang',
							data: {cabang_id: data.id, task : 'DELETE'},
							success: function(response){
								if(response==1){
									grid_cabang.trigger('reloadGrid');
									$("#grid-cabang").trigger('reloadGrid');
								}
							}
						});
					}
				});
			}
		}); 
		grid_cabang.jqGrid('navGrid','#pgrid-cabang',{edit:false,add:false,del:false,search:false});
		grid_cabang.jqGrid('filterToolbar',{});
		
		form_cabang.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */
		
		btnadd_cabang.click(function(){
			task_cabang.val("INSERT");
			form_cabang.dialog("open");
		});
		
		btncancel_cabang.click(function(){
			form_cabang.dialog("close");
		});
		
		btnsave_cabang.click(function(){
			data = form_cabang.serializeObject();
			$.ajax({
				url : "c_cabang",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_cabang.trigger('reloadGrid');
				btncancel_cabang.click();
			});
		});
		
	});
</script>

	<table id="grid-cabang" class="table table-striped table-bordered" ></table>
	<div id="pgrid-cabang"></div>
	
	<button id="btn-add-cabang">Tambah</button>
	
	<form id="form-cabang" title="cabang" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_cabang">
	   <table>
			<tr style="display: none">
				<td align="right">cabang_id:</td>
				<td align="left"><input id="cabang_id" name="cabang_id" /></td>
			</tr>
			
			<tr > <td align="right">cabang_nama:</td> <td align="left"><input id="cabang_nama" name="cabang_nama" /></td> </tr>
			<tr > <td align="right">cabang_aktif:</td> <td align="left"><input id="cabang_aktif" name="cabang_aktif" /></td> </tr>
			
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-cabang" value="Save"  />
					<input id="btn-cancel-cabang" type="reset" value="Cancel" /></td>
			</tr>
		</table>
	</form>